/**
 * Created by xxcao on 2017/6/15.
 */


var sql = require('./sqlMapping');
var util = require('../util/util');
var code = require('../util/code');

module.exports = {
    get:function (req, res, next) {
        if (util.isParameterEmpty(req.query.user_id)){
            res.json(util.toJson(code.PARAMETER_IS_NULL, '参数不能为空'));
            return;
        }
        if (!util.isValidSession(req, 'GET')) {
            res.json(util.toJson(code.SESSION_VALID, 'session失效，请重新登录'));
            return;
        }
        mysqlPool.connection(function (err, connection) {
            if (err){
                throw err;
            }
            var params = null;
            var sqlStr = null;
            if(util.isParameterEmpty(req.query.board_id)) {
                sqlStr = sql.news.finds;
                params = [req.query.user_id];
            } else {
                sqlStr = sql.news.findsWithBoardId;
                params = [req.query.user_id, req.query.board_id];
            }
            if(util.isParameterEmpty(req.query.page) || isNaN(Number(req.query.page))){
                connection.query(sqlStr, params, function (err, results) {
                    if (err) {
                        res.json(util.toJson(code.DATABASE_SQL_EXCUTE_ERROR, '获取我的动态失败'));
                        connection.release();
                    } else {
                        res.json(util.toJson(code.OPERATE_SUCCESS, '获取我的动态成功', results));
                        connection.release();
                    }
                });
            }
            else {
                new Promise(function (resolve) {
                    if(util.isParameterEmpty(req.query.board_id)) {
                        sqlStr = sql.news.findsCount;
                    } else {
                        sqlStr = sql.news.findsWithBoardIdCount;
                    }
                    connection.query(sqlStr, params, function (err, results) {
                        if (err){
                            res.json(util.toJson(code.DATABASE_SQL_EXCUTE_ERROR, '获取我的动态失败'));
                            connection.release();
                        } else {
                            resolve(results);
                        }
                    })
                }).then(function (result) {
                    return new Promise(function (resolve) {
                        var pageSize = util.pageSize();
                        var page = Number(req.query.page);
                        page = Math.min(page, Math.ceil(Number(result[0]['count']) / pageSize));
                        page = Math.max(page, 1);
                        if(util.isParameterEmpty(req.query.board_id)) {
                            sqlStr = sql.news.findsPage;
                        } else {
                            sqlStr = sql.news.findsWithBoardIdPage;
                        }
                        params.push(pageSize);
                        params.push((page - 1) * pageSize);
                        connection.query(sqlStr, params, function (err, reuslts) {
                            if (err){
                                res.json(util.toJson(code.DATABASE_SQL_EXCUTE_ERROR, '获取我的动态失败'));
                                connection.release();
                            } else {
                                res.json(util.toJson(code.OPERATE_SUCCESS, '获取我的动态成功', reuslts));
                                connection.release();
                            }
                        })
                    });
                });
            }
        });
    },
};